/**
 *
 * @author zzhijie
 * @date 2021-04-25
 */
import FBtn from "./btn";

export default {
  name: "f-add-btn",
  props: {
    permission: {
      type: String,
      default: ''
    },
    config: {
      type: Object,
      default: () => ({})
    },
  },
  components: {FBtn},
  data() {
    return {
      crudConfig: this.$getGlobalConfig('crud'),
    }
  },
  methods: {
    getCrud(parent) {
      if (parent && parent.handleCustomEvent) {
        return parent;
      } else if (parent.$parent) {
        return this.getCrud(parent.$parent);
      }
    },
    handleClick() {
      let crud = this.getCrud(this.$parent);
      if (crud) {
        crud.handleCustomEvent({event: "add"});
      }
    }
  },
  render() {
    let addBtn = this.config.$addBtn || this.crudConfig.$addBtn || {
      hide: false,
      text: '新增',
      props: {
        type: 'primary',
        icon: 'el-icon-plus',
        plain: true
      },
      attrs: {}
    };
    return !addBtn.hide && <f-btn prop={addBtn.props} attrs={addBtn.attrs} on-click={this.handleClick} permission={this.permission}>{this.$slots.default || addBtn.text}</f-btn>
  }
}
